/**    
 * @{#} PriceSettingActivity.java Create on 2014年11月4日 下午9:41:18    
 *    
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 * @description
 *	
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.OActionSheet;
import cn.o.app.ui.OActionSheet.OActionItem;
import cn.o.app.ui.OActionSheet.OnActionItemClickListener;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.widget.ItemView;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.CostDoctorAppointTask;
import com.smiier.skin.net.CostDoctorAppointTask.CostDoctorAppointItem;
import com.smiier.skin.net.CostDoctorAppointTask.CostDoctorAppointRequest;
import com.smiier.skin.net.CostDoctorAppointTask.CostDoctorAppointResponse;
import com.smiier.skin.net.CostDoctorAppointTask.DoctorAppointItem;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class PriceSettingActivity extends BasicActivity {

	private ItemView mItemviewSettingPriceValue;

	private ItemView mItemviewInvitePatientPrice;
	CostDoctorAppointItem curappointitem = null;
	double price;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_price_setting);

		mItemviewSettingPriceValue = (ItemView) findViewById(R.id.itemview_set_ask_price);
		mItemviewInvitePatientPrice = (ItemView) findViewById(R.id.itemview_invite_patient_price);

		init();

		setNavTitle("咨询价格设置");
		setNavRightBtn("保存");
	}

	@Override
	protected void onResume() {
		super.onResume();

		// MobclickAgent.onResume(this);

		try {
			if (!CommonUtility.isNull(GlobalSettings.sUser)) {
				JSONArray costList = new JSONArray(
						JsonUtil.convert(GlobalSettings.sUser.CostList));
				for (int i = 0; i < costList.length(); i++) {
					JSONObject object = costList.getJSONObject(i);
					int key = object.getInt("Key");
					if (key == 1) {
						mItemviewSettingPriceValue.setTextValueValue("￥"
								+ object.getString("Value"));
						price = Double.parseDouble(object.getString("Value"));
					} else if (key == 2) {
						mItemviewInvitePatientPrice.setTextValueValue("￥"
								+ object.getString("Value"));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		getPriceList();

	}

	private void getPriceList() {
		// 获取咨询价格配置
		if (GlobalSettings.mDoctorAppoints == null
				|| GlobalSettings.mDoctorAppoints.size() <= 0) {
			CostDoctorAppointTask task = new CostDoctorAppointTask();
			task.addListener(new NetTaskListener<CostDoctorAppointTask.CostDoctorAppointRequest, CostDoctorAppointTask.CostDoctorAppointResponse>() {

				@Override
				public void onException(
						INetTask<CostDoctorAppointRequest, CostDoctorAppointResponse> task,
						Exception e) {

				}

				@Override
				public void onComplete(
						INetTask<CostDoctorAppointRequest, CostDoctorAppointResponse> task,
						CostDoctorAppointResponse response) {
					if (response != null && response.size() > 0) {
						GlobalSettings.mDoctorAppoints = response;
					}
				}
			});
			add(task);

		}
	}

	private void showSelectPrice() {
		if (GlobalSettings.mDoctorAppoints == null) {
			getPriceList();
		} else {
			ArrayList<OActionItem> dataProvider = new ArrayList<OActionSheet.OActionItem>();
			OActionItem item = null;
			boolean has = false;
			StringBuilder sb = new StringBuilder();
			ArrayList<String> prof = GlobalSettings.sUser.Professional;
			for (String str : prof) {
				sb.append(str).append(",");
			}

			for (CostDoctorAppointItem appointitem : GlobalSettings.mDoctorAppoints) {
				if (sb.toString().contains(appointitem.Professional)) {
					has = true;
					curappointitem = appointitem;
					break;
				}
			}
			if (curappointitem == null) {
				curappointitem = GlobalSettings.mDoctorAppoints.get(0);
			}
			for (DoctorAppointItem cost : curappointitem.CostList) {
				item = new OActionItem();
				item.setText(cost.Title + "");
				dataProvider.add(item);
			}
			OActionSheet<OActionItem> actionSheet = new OActionSheet<OActionSheet.OActionItem>(
					PriceSettingActivity.this);
			actionSheet.setCancel("取消");
			actionSheet.setDataProvider(dataProvider);
			actionSheet.show(false, true);
			actionSheet
					.setOnActionItemClickListener(new OnActionItemClickListener<OActionSheet.OActionItem>() {

						@Override
						public void onItemClick(
								OActionSheet<OActionItem> sheet, View v,
								int position, OActionItem dataItem) {
							price = curappointitem.CostList.get(position).Cost;
							mItemviewSettingPriceValue.setTextValueValue("￥"
									+ price);
						}
					});
		}
	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.itemview_set_ask_price) {
			showSelectPrice();
			/*
			 * Intent intent = new Intent(activity,
			 * UpdateInfoInputActivity.class);
			 * intent.putExtra(Constant.IDENTITY_KEY_PARAM_KEY, "Cost");
			 * intent.putExtra(Constant.IDENTITY_KEY_1,
			 * InputType.TYPE_CLASS_NUMBER);
			 * intent.putExtra(Constant.IDENTITY_KEY_NAV_TITLE, "设置问诊价格");
			 * intent.putExtra(Constant.IDENTITY_KEY_2, new int[] { 0, 10000 });
			 * startActivity(intent);
			 */

		} else if (id == R.id.itemview_invite_patient_price) {
			startActivity(new Intent(activity,
					InvitePatientPriceSettingActivity.class));
		} else if (id == R.id.text_right) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("Cost", price);
			params.put(Constant.PARAM_API, "User.Set");
			params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
			RequestTaskIntercept.requestIntercept(activity,
					Constant.URL.REQUEST, new RequestHandler(activity,
							new HandlerCallback() {

								@Override
								public void callback(Object o) {
									if (!CommonUtility.isNull(o)) {
										JSONObject object = (JSONObject) o;
										try {
											int resultCode = object
													.getInt(Constant.JSON_PARAM_RESCODE);
											if (CommonUtility
													.response200(object)) {
												CommonUtility.toast(activity,
														"保存成功");
												try {

													GlobalSettings.sUser = JsonUtil.convertFromObject(
															object.getJSONObject("Res"),
															User.class);

													finish();
												} catch (Exception e) {
													e.printStackTrace();
												}
											} else if (resultCode == -2005) {
												CommonUtility
														.tip(activity,
																"您已经认证或者正在等待审核认证中，不可修改数据。");
											} else {
												CommonUtility.tip(activity,
														"修改失败，请稍后再试。");
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							}), params);
		}
	}

	/*
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */
}
